/*
 * Copyright (C) 2014-2016 Qiujuer <qiujuer@live.cn>
 * WebSite http://www.qiujuer.net
 * Author qiujuer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.qiujuer.genius.ui.widget;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.BlurDrawLooper;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import net.qiujuer.genius.ui.drawable.TouchEffectDrawable;

/**
 * FloatActionButton
 *
 * @since 2021-05-10
 */
public class FloatActionButton extends Component implements TouchEffectDrawable.PerformClicker,
    TouchEffectDrawable.PerformLongClicker, Component.TouchEventListener, Component.ClickedListener {
    private static final int DIR_SCREEN_TOP = 1700;
    private int mShadowRadius;
    private TouchEffectDrawable mTouchDrawable;
    private Paint mPaint;
    private Paint mLinePaint;
//    private int mWidth;
//    private int mHeight;
    private int mXCenter;
    private int mYCenter;
//    private Context context;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public FloatActionButton(Context context) {
        this(context, null);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 自定义属性
     */
    public FloatActionButton(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 自定义属性
     * @param defStyleAttr res资源
     */
    public FloatActionButton(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        this.context = context;
        init(attrs, defStyleAttr, 0);
        intiListener();
    }

    private void intiListener() {
        setLayoutRefreshedListener(new LayoutRefreshedListener() {
            @Override
            public void onRefreshed(Component component) {
                int[] locationOnScreen = getLocationOnScreen();
                if (locationOnScreen[1] < DIR_SCREEN_TOP) {
                    setVisibility(Component.INVISIBLE);
                } else {
                    setVisibility(Component.VISIBLE);
                }
            }
        });
    }

    private void init(AttrSet attrs, int defStyleAttr, int defStyleRes) {
        if (attrs == null) {
            return;
        }
//        mWidth = AttrHelper.vp2px(56, getContext());
//        mHeight = AttrHelper.vp2px(56, getContext());
        final int padding = mShadowRadius;
        // set padding so the inner image sits correctly within the shadow.
        setPadding(Math.max(padding, getPaddingLeft()),
            Math.max(padding, getPaddingTop()),
            Math.max(padding, getPaddingRight()),
            Math.max(padding, getPaddingBottom()));
        initPaint();
        addDrawTask(mDrawTask);
        setClickedListener(this);
        setTouchEventListener(this);
    }

    private void initPaint() {
        mPaint = new Paint();
        //画笔样式
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        mPaint.setAntiAlias(true);
        //画笔宽度
        mPaint.setStrokeWidth(2);
        mLinePaint = new Paint();
        mLinePaint.setStyle(Paint.Style.FILL_STYLE);
        mLinePaint.setStrokeCap(Paint.StrokeCap.BUTT_CAP);
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStrokeWidth(10);
    }

    @Override
    public void setPadding(int left, int top, int right, int bottom) {
        left = Math.max(mShadowRadius, left);
        top = Math.max(mShadowRadius, top);
        right = Math.max(mShadowRadius, right);
        bottom = Math.max(mShadowRadius, bottom);
        super.setPadding(left, top, right, bottom);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        final TouchEffectDrawable d = mTouchDrawable;
        if (d != null && isEnabled()) {
            d.onTouch(touchEvent);
        }
        return false;
    }


    private DrawTask mDrawTask = new DrawTask() {
        @Override
        public void onDraw(Component component, Canvas canvas) {
            mXCenter = component.getWidth() / 2;
            mYCenter = component.getHeight() / 2;
            //画笔设置阴影效果
            mPaint.setBlurDrawLooper(new BlurDrawLooper(18, 0, 0, new Color(Color.getIntColor("#42D1D4"))));
            canvas.drawCircle(mXCenter, mYCenter, 80, mPaint, new Color(Color.getIntColor("#00BCD4")));
            canvas.drawLine(mXCenter - 30, mYCenter, mXCenter + 30,
                mYCenter, mLinePaint, new Color(Color.getIntColor("#ffffff")));
            canvas.drawLine(mXCenter, mYCenter - 30, mXCenter,
                mYCenter + 30, mLinePaint, new Color(Color.getIntColor("#ffffff")));
        }
    };

    @Override
    public void postPerformClick() {
    }

    @Override
    public void postPerformLongClick() {
    }

    /**
     * Get the TouchEffect drawable,
     * you can set parameters in this
     *
     * @return See {@link TouchEffectDrawable}
     */
    @SuppressWarnings("unused")
    public TouchEffectDrawable getTouchDrawable() {
        return mTouchDrawable;
    }

    @Override
    public void onClick(Component component) {
        new ToastDialog(getContext()).setText("FlaotActionButton OnClick.").setDuration(2000).show();
    }
}
